#include "spectrogramplot.h"

SpectrogramPlot::SpectrogramPlot(QWidget *parent)
{

}

void SpectrogramPlot::plotSpectrogram(const QVector<double>& T,
                                      const QVector<double>& F,
                                      const QVector<QVector<double>>& PSD,
                                      const QVector<double>& VLF,
                                      const QVector<double>& LF,
                                      const QVector<double>& HF,
                                      bool flagWavelet) {
    // 清空图形
    clearPlottables();

    // 数据转换
    QVector<double> freqAxis = F;
    if (flagWavelet) {
        std::transform(F.begin(),  F.end(),  freqAxis.begin(),
                       [](double f) { return 1.0 / f; }); // 转换为周期
    }

    // 创建热力图
    mColorMap = new QCPColorMap(xAxis, yAxis);
    mColorMap->data()->setSize(T.size(),  freqAxis.size());
    mColorMap->data()->setRange(
        QCPRange(T.first(),  T.last()),
        flagWavelet ? QCPRange(log2(freqAxis.last()),  log2(freqAxis.first()))
                    : QCPRange(freqAxis.first(),  freqAxis.last())
        );

    // 填充数据（PSD单位转换：s²/Hz）
    for (int x = 0; x < T.size();  ++x) {
        for (int y = 0; y < freqAxis.size();  ++y) {
            mColorMap->data()->setCell(x, y, PSD[x][y] / 1e6); // 转换为s²/Hz
        }
    }

    // 设置坐标轴
    if (flagWavelet) {
        yAxis->setScaleType(QCPAxis::stLogarithmic);
        // yAxis->setScaleLogBase(2);
        yAxis->setNumberFormat("eb");
        yAxis->setLabel("Period (s)");

        // 自定义刻度标签（显示原始周期值）
        QSharedPointer<QCPAxisTickerLog> logTicker(new QCPAxisTickerLog);
        logTicker->setLogBase(2);
        yAxis->setTicker(logTicker);
        yAxis->setRangeReversed(true); // Y轴反向
    } else {
        yAxis->setScaleType(QCPAxis::stLinear);
        yAxis->setLabel("Frequency (Hz)");
    }
    xAxis->setLabel("Time (s)");

    // 颜色映射
    mColorMap->setGradient(QCPColorGradient::gpJet);
    mColorScale = new QCPColorScale(this);
    plotLayout()->addElement(0, 1, mColorScale);
    mColorMap->setColorScale(mColorScale);

    // 绘制频带参考线
    auto addBandLine = [this](double yPos, bool isWavelet) {
        QCPItemLine *line = new QCPItemLine(this);
        line->start->setCoords(xAxis->range().lower, isWavelet ? log2(1.0/yPos) : yPos);
        line->end->setCoords(xAxis->range().upper, isWavelet ? log2(1.0/yPos) : yPos);
        line->setPen(QPen(Qt::white, 1, Qt::DashLine));
    };

    if (!flagWavelet) {
        addBandLine(VLF[1], false); // VLF上限
        addBandLine(LF[1], false);  // LF上限
        addBandLine(HF[1], false);  // HF上限
    }

    // 交互设置
    setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    rescaleAxes();
}
void SpectrogramPlot::mouseDoubleClickEvent(QMouseEvent *event) {
    // 复制图像到剪贴板
    QPixmap pixmap = toPixmap(width(), height());
    QApplication::clipboard()->setPixmap(pixmap);

    // 可选：显示提示
    QToolTip::showText(event->globalPos(), "Spectrogram copied to clipboard!", this);
}
